Create and modify a report

In this example, we'll create and modify a simple report using the Report Designer Component. This will guide you through the basic steps in connecting to a data source and creating a report. The report you will be creating is a Sales by Region report based on a Microsoft Access database. We'll use the Report Experts to help create the report quickly and easily, then we'll modify some of its properties and make it more interactive by adding some event-driven code.

This exercise only touches on a few of the hundreds of properties and methods available but will give you an idea of how simple it is to fully integrate and customize reports in your Visual Basic projects.

To Add the RDC to your project
  1. Open Visual Basic and create a new Standard EXE project by selecting Standard EXE from the start up dialog or selecting it from New Project under the File menu.
  2. Add the Report Designer Component to Visual Basic if not already added during the installation process. 
  3. From the Project menu, select Components.
  4. Click the Designers tab and check Crystal Reports 8. Click Apply and then click Close. The Report Designer is now available in this project and any projects you create in the future.
  5. Now we need to insert the Report Designer into the project form. On the Project menu, point to More ActiveX Designers and then click Add Crystal Reports 8.5. (In some environments, Add Crystal Reports 8.5 will appear as an option itself on the Project menu.)
To select a data source
  1. Check Using Report Expert and select Standard. Click OK. The Standard Report Expert appears. This window allows you to select from either Project or Other data sources. Typically, you use a project-based data source. The Other option enables you to use the native data drivers included with Crystal Reports.
  2. We'll connect to the ODBC data source using ADO. Click the Project button.
  3. The Select Data Source window appears with ODBC selected by default. Select Xtreme Sample Database from the list of data sources. To see which options are available, click Advanced. The Advanced Options dialog appears. Click OK to accept the default of connecting using ADO.
  4. Now that you've specified the data source and connection method, you need to specify which tables to use. Click Next in the Select Data Dialog. This brings up the Select Recordset dialog.
  5. Select Customer from the object list and click OK to return to the Data Tab. The item ado should be displayed in the Tables list box.
To create a report
  1. Now that you've selected the database and the table, you need to specify the fields to include in the report. Click Next to move to the Fields tab.
  2. Select the Customer Name database field from the Available Fields list, then Click Add to add the field into the Fields to Display box. Do the same for the Last Year's Sales, City and Region fields.
  3. Click on the Group tab and select the Region field, then click Add to add the field into the Group By box. Do the same for the City field. This will group your data by Region and, within each region, by City. By default, the Sort Order for the Region and City Fields is in Ascending Order.
  4. Click the Total tab. Because Last Year's Sales is the only numeric field in the report, the Report Expert automatically selects it for totaling.
  5. Click the Top N tab. For the Region Tab, choose Sort All Groups based on Sum of Last Year's Sales. Do the same for the City tab.
  6. Click the Chart tab and click the Pie in the Chart Type list on the Chart|Type tab. Select the pie chart with 3D visual effect.
  7. Click the Chart|Data tab. The Report Expert automatically selects to create the chart based on the sum of Last Year's Sales.
  8. Click the Chart|Text tab. In the Title box, type "Sales by Region".
  9. Finally, to give the report a professional look, go to the Style tab in the main set of Expert tabs and select the Executive, Trailing Break style. Click Finish. The RDC creates your report.
To add the Report Viewer to your project
  1. The Report Expert presents you with the option of adding a form with the Crystal Report Viewer control and setting this as the start-up object. Click OK to accept the defaults. The Expert will format the report and display it in the design window.
  2. Click the Start button on the Visual Basic toolbar or press F5 to run your project. After a few seconds, you will see a form displaying the finished report in the Crystal Report Viewer.

This exercise has shown the basic steps for creating a new report. Although you don't always have to use the Report Experts, they make connecting to your data source and creating the initial report fast and easy. You can then alter the look and feel of the report using the Report Designer window. Common tasks like field formatting, adding text and modifying field positions can be accomplished by dragging fields or altering their properties. These can be set using Visual Basic code or in the Visual Basic object properties window.



Seagate Software IMG Holdings, Inc.
http://www.seagatesoftware.com
Support services:
http://support.seagatesoftware.com